<template>
	<yd-layout id='bdStageTopic' class=''>
		<div class="content">
			<div class="topic">
				<div class="header">
					<div class="title_name c_size_34_48">{{dataObj.title}}</div>
					<div class="topic_num c_size_30_42">（共{{topicList.length}}题）</div>
				</div>
				<div class="fg">
					<span></span>
					<div class="xian"></div>
					<span class="span"></span>
				</div>
				<div class="lsit_topic">
					<div class="list" v-for="item,index in topicList" :key='index'>
						<div class="list_title">{{item.title}}</div>
						
						<!--多选题--> 
						<div v-if="item.kind==1">
							<div :key='num' @click="moreTopic(item,i,index,num)" class="list_item" v-for="i,num in item.questionArr">
								<img v-if="i.a" src="../../../assets/bd_img/yes.png" alt="" />
								<span v-else ></span>
								<div class="item_title c_size_30_42">{{num===0?'A、':num==1?'B、':num==2?'C、':'D、'}}{{i.q}}</div>
							</div>
						</div>
						<!--单选题-->
						<div v-else>
							<div @click="dTopic(item,i,index,num)" class="list_item" v-for="i,num in item.questionArr">
								<img v-if="item.dAnswer[0]===num" src="../../../assets/bd_img/yes.png" alt="" />
								<span v-else  ></span>
								<div class="item_title c_size_30_42">{{num===0?'A、':num==1?'B、':num==2?'C、':'D、'}}{{i.q}}</div>
							</div>
						</div>
					</div>
				</div>
					
			</div>
			<div class="btn" @click="goResult">提交查看结果</div>
		</div>
	</yd-layout>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				topicList:[],
				dataObj:{},
			}
		},
		created() {},
		watch: {

		},
		computed: {

		},
		async mounted() {
			document.title = '阶段总结测评';
			let flg =await this.isActive()
			if(flg){
				this.getExam()
			}
			
		},
		methods: {
			isActive(){
				return new Promise((resolve,reject)=>{
					this.$root.ajax({
						name:'bible/isActive',
						type:'get',
						closeAllLoading:true,
						params: {}
					}).then((d) => {
						if(d.data){
							resolve(true)
						}else{
							this.$router.push({
								name:'bd_index'
							})
						}
						
					})
				})
			},
			//多选题item
			moreTopic(item,i,index,num){
				if(i.a){
					i.a=false;
					for(let m=0;m<item.questionArr.length;m++){
						if(item.questionArr[m].a){
							item.moreAnswerFlg=true
							break
						}else{
							item.moreAnswerFlg=false
						}
					}
				}else{
					i.a=true
					item.moreAnswerFlg=true
				}
				this.$set(this.topicList,index,item)
			},
			dTopic(item,i,index,num){
				item.dAnswer=[];
				item.dAnswer.push(num)
				this.$set(this.topicList,index,item)
			},
			//单选题
			//获取题目
			getExam(){
				this.$root.ajax({
					name: 'bible/exam',
					type:'get',
					params: {
						level_id:this.$route.query.id
					}
				}).then((d) => {
					this.dataObj=Object.assign({},this.dataObj,d.data)
					for(let a =0;a<d.data.topics.length;a++){
						d.data.topics[a].moreAnswerFlg=false //判断多选是否已经做了
						d.data.topics[a].questionArr=[]
						d.data.topics[a].questionArr.push({q:d.data.topics[a].option_a,a:false,str:'A'})
						d.data.topics[a].questionArr.push({q:d.data.topics[a].option_b,a:false,str:'B'})
						d.data.topics[a].questionArr.push({q:d.data.topics[a].option_c,a:false,str:'C'})
						d.data.topics[a].questionArr.push({q:d.data.topics[a].option_d,a:false,str:'D'})
						d.data.topics[a].dAnswer=[]
					}
					this.topicList=d.data.topics
				})
			},
			//重组题目数组
			restTopicArr(){
				
			},
			goResult(){
				let contentJosn={};
				let noTopic=[] //记录没有做过的题
				let content={}
				for(let a = 0;a<this.topicList.length;a++){
					if(this.topicList[a].kind==1){
						//多选
						if(!this.topicList[a].moreAnswerFlg){
							noTopic.push(this.topicList[a])
						}
						let moreArr=[];
						for(let m=0;m<this.topicList[a].questionArr.length;m++){
							if(this.topicList[a].questionArr[m].a){
								moreArr.push(this.topicList[a].questionArr[m].str)
							}
						}
						content[(a+1)]=moreArr
					}else{
						//单选
						if(this.topicList[a].dAnswer.length==0){
							noTopic.push(this.topicList[a])
						}
						let daArr=[];
						let str=this.topicList[a].dAnswer[0]==1?'B':this.topicList[a].dAnswer[0]==2?'C':this.topicList[a].dAnswer[0]==3?'D':'A'
						daArr.push(str)
						content[(a+1)]=daArr
					}
				}
				if(noTopic.length>0){
					this.$dialog.toast({
	                    mes:`您的第${noTopic[0].idx}题没有选择答案!`,
	                    timeout: 1500,
	                });
				}else{
					console.log(content)
					this.$root.ajax({
						name: 'bible/doExam',
						params: {
							id:this.dataObj.id,
							content:JSON.stringify(content),
							decode:1,
						}
					}).then((d) => {
						this.$router.push({
							name:'bd_result',
							query:{
								exam_id:this.dataObj.id,
								id:this.$route.query.id
							}
						})
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	@import './stageTopic.scss';
</style>